package com.cooba.activitys.Living;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.cooba.R;
import com.cooba.bean.LivingDetailBean;
import com.cooba.utils.ServerURL;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class LivingContentAddTitleActivity extends AppCompatActivity implements View.OnClickListener {

    private ImageButton add_content_new_back_ib;
    private TextView add_content_new_next_tv;
    private TextView add_content_new_finish_tv;
    private EditText add_content_new_title_et;
    private HttpUtils httpUtils;
    private RequestParams params;
    private String livie_id;
    private List<LivingDetailBean.LivingDetailContent> list=new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_living_add_content_new);
        livie_id = getIntent().getStringExtra("live_id");
        initView();
        initListener();
    }

    private void initListener() {
        add_content_new_back_ib.setOnClickListener(this);
        add_content_new_next_tv.setOnClickListener(this);
        add_content_new_finish_tv.setOnClickListener(this);
    }

    private void initView() {
        add_content_new_back_ib = (ImageButton) findViewById(R.id.add_content_new_back_ib);
        add_content_new_next_tv = (TextView) findViewById(R.id.add_content_new_next_tv);
        add_content_new_finish_tv = (TextView) findViewById(R.id.add_content_new_finish_tv);
        add_content_new_title_et = (EditText) findViewById(R.id.add_content_new_title_et);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.add_content_new_back_ib:
                finish();
                break;
            case R.id.add_content_new_next_tv:
                if (!TextUtils.isEmpty(add_content_new_title_et.getText().toString().trim())) {
                    addList();
                    Intent intent = new Intent(this, LivingContentTypeActivity.class);
                    intent.putExtra("living_info_list", (Serializable) list);
                    startActivity(intent);
                }else {
                    Toast.makeText(this,"标题不能为空!",Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.add_content_new_finish_tv:
                if (!TextUtils.isEmpty(add_content_new_title_et.getText().toString().trim())) {
                    doSubmit();
                }else {
                    Toast.makeText(this,"标题不能为空!",Toast.LENGTH_SHORT).show();
                }
                break;
            default:
                break;
        }
    }

    private void addList() {
        LivingDetailBean.LivingDetailContent content=new LivingDetailBean.LivingDetailContent();
        content.setLiveId(livie_id);
        content.setMessageType("0");
        content.setIsTitle("1");
        content.setMessage(add_content_new_title_et.getText().toString().trim());
        list.add(content);
    }

    private void doSubmit() {
        httpUtils = new HttpUtils();
        params = new RequestParams();
        params.addBodyParameter("live_id", livie_id);
        params.addBodyParameter("message_type", "0");
        params.addBodyParameter("is_title", "1");
        params.addBodyParameter("width_height", "1");
        params.addBodyParameter("message", add_content_new_title_et.getText().toString().trim());
        String urlmylivingadd = ServerURL.httpUrl + ServerURL.mylivingadd;
        httpUtils.send(HttpRequest.HttpMethod.POST, urlmylivingadd, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                Log.d("wei", "发布内容成功" + responseInfo.result);

                JSONObject jsonObject = null;
                try {
                    jsonObject = new JSONObject(responseInfo.result.toString());
                    String status = jsonObject.get("status").toString();
                    if (status.equals("1")) {
                        setResult(RESULT_OK);
                        finish();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(HttpException error, String msg) {
                Log.d("wei", "发布内容失败" + msg);
            }
        });
    }
}
